package com.jiezheng.huinong.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.jiezheng.huinong.db.conf.IApplyManageDAO;
import com.jiezheng.huinong.model.ApplyDetail;
import com.jiezheng.huinong.model.ApplyIndex;
import com.jiezheng.huinong.model.ApplyIndexPrint;
import com.jiezheng.huinong.model.ApplyInfo;
import com.jiezheng.huinong.model.IndexInfo;
import com.jiezheng.huinong.model.PaymentVouchers;
import com.jiezheng.huinong.util.DbUtil;

public class ApplyManageDAOImpl implements IApplyManageDAO {

	@Override
	public void addApply(ApplyInfo apply) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.applyManage.addApply", apply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	
	@Override
	public void addApplyDetail(ApplyDetail apply) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.applyManage.addApplyDetail", apply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public void deleteApply(String applyNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.applyManage.deleteApply", applyNo);
			session.insert("com.jiezheng.huinong.applyManage.deleteApplyDetail", applyNo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}	
		
	}
	
	@Override
	public void modifyApply(ApplyInfo apply) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.applyManage.modifyApply",
					apply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	public Integer getTotalApplyDetailNum(String applyNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.applyManage.getTotalApplyDetailNum", applyNo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}
	
	@Override
	public List<ApplyDetail> getApplyDetailInfo(String applyNo, Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<ApplyDetail> applies = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("applyNo", applyNo);
		
		try {
			applies = session.selectList(
					"com.jiezheng.huinong.applyManage.getApplyDetailByPage", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return applies;
	}
	
	@Override
	public String getMaxApplyNo() {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		String maxApplyNo = "";
		try {
			maxApplyNo = session
					.selectOne("com.jiezheng.huinong.applyManage.getMaxApplyNo");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		return maxApplyNo == null ? "01000000" : maxApplyNo;
	}

	@Override
	public Integer getTotalApplyNum(ApplyInfo cond, List<Integer> statusRange) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("deptNo", cond.getDeptNo());
		rowInfo.put("statusRange", statusRange);
		
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.applyManage.getTotalApplyNumByStatus", rowInfo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<ApplyInfo> getApplyInfo(ApplyInfo cond, List<Integer> statusRange, Integer startRow,
			Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<ApplyInfo> applies = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("statusRange", statusRange);
		rowInfo.put("deptNo", cond.getDeptNo());
		
		try {
			applies = session.selectList(
					"com.jiezheng.huinong.applyManage.getApplyInfoByPageByStatus", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return applies;
	}

	@Override
	public ApplyInfo getApplyByApplyNo(String applyNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		ApplyInfo apply = null;
		try {
			apply = session
					.selectOne("com.jiezheng.huinong.applyManage.getApplyInfoByApplyNo", applyNo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return apply;
		
	}

	@Override
	public void jointIndex(ApplyInfo apply) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.applyManage.jointIndex",
					apply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	
	@Override
	public void unJointIndex(ApplyInfo apply) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.applyManage.unJointIndex",
					apply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	

	@Override
	public Integer getTotalApplyIndexNum(IndexInfo cond, int status) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.applyManage.getTotalApplyIndexNumByStatus", cond);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<ApplyIndex> getApplyIndexInfo(IndexInfo cond, int status, Integer startRow,
			Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<ApplyIndex> applies = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("status", status);
		rowInfo.put("indexNo", cond.getIndexNo());
		rowInfo.put("budgetUnit", cond.getBudgetUnit());
		rowInfo.put("indexSource", cond.getIndexSource());
		rowInfo.put("indexType", cond.getIndexType());
		rowInfo.put("fundProp", cond.getFundProp());
		rowInfo.put("payMethod", cond.getPayMethod());
		rowInfo.put("budgetItem", cond.getBudgetItem());
		rowInfo.put("funcType", cond.getFuncType());
		rowInfo.put("financialType", cond.getFinancialType());
		rowInfo.put("settleMethod", cond.getSettleMethod());
		rowInfo.put("budgetType", cond.getBudgetType());
		rowInfo.put("paperDate", cond.getPaperDate());
		rowInfo.put("deptNo", cond.getDeptNo());
		
		try {
			applies = session.selectList(
					"com.jiezheng.huinong.applyManage.getApplyIndexByPageByStatus", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return applies;
	}
	
	public List<ApplyIndexPrint> getApplyIndexInfo(int status, List<String> applyNos) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<ApplyIndexPrint> applies = null;
		
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("status", status);
		rowInfo.put("applyNos", applyNos);
		
		try {
			applies = session.selectList(
					"com.jiezheng.huinong.applyManage.getApplyIndexByStatus", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return applies;
	}
	
	public List<PaymentVouchers> getPaymentVouchers(int status, List<String> applyNos) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<PaymentVouchers> applies = null;
		
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("status", status);
		rowInfo.put("applyNos", applyNos);
		
		try {
			applies = session.selectList(
					"com.jiezheng.huinong.applyManage.getPaymentVouchers", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return applies;
	}

	@Override
	public void changeStatus(ApplyInfo apply) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();

		try {
			session.update("com.jiezheng.huinong.applyManage.changeStatus",
					apply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public Integer getTotalToAuditApplyNum(IndexInfo cond) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.applyManage.getTotalToAuditApplyNum", cond);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<ApplyIndex> getToAuditApplyInfo(IndexInfo cond, Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<ApplyIndex> applies = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("indexNo", cond.getIndexNo());
		rowInfo.put("budgetUnit", cond.getBudgetUnit());
		rowInfo.put("indexSource", cond.getIndexSource());
		rowInfo.put("indexType", cond.getIndexType());
		rowInfo.put("fundProp", cond.getFundProp());
		rowInfo.put("payMethod", cond.getPayMethod());
		rowInfo.put("budgetItem", cond.getBudgetItem());
		rowInfo.put("funcType", cond.getFuncType());
		rowInfo.put("financialType", cond.getFinancialType());
		rowInfo.put("settleMethod", cond.getSettleMethod());
		rowInfo.put("budgetType", cond.getBudgetType());
		rowInfo.put("paperDate", cond.getPaperDate());
		rowInfo.put("deptNo", cond.getDeptNo());
		
		try {
			applies = session.selectList(
					"com.jiezheng.huinong.applyManage.getToAuditApplyInfo", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return applies;
	}

	@Override
	public List<ApplyDetail> getApplyDetailInfo(String applyNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<ApplyDetail> applies = null;
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("applyNo", applyNo);
		
		try {
			applies = session.selectList(
					"com.jiezheng.huinong.applyManage.getAllApplyDetail", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return applies;
	}

	@Override
	public String getMaxShiboNo() {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		String maxApplyNo = "";
		try {
			maxApplyNo = session
					.selectOne("com.jiezheng.huinong.applyManage.getMaxShiboNo");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		return maxApplyNo == null ? "02000000" : maxApplyNo;
	}

	@Override
	public void updateShiboNo(String shiboNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.applyManage.modifyShiboNo",
					shiboNo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public void changePayStatus(ApplyInfo apply) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();

		try {
			session.update("com.jiezheng.huinong.applyManage.changePayStatus",
					apply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
		
	}
}
